package com.gxa.service.shezhiservice.Impl;

import com.gxa.common.R;
import com.gxa.mapper.shezhimapper.ShangPinMapper1;
import com.gxa.pojo.ShangPin;
import com.gxa.pojo.ShangPin_SheZhi;
import com.gxa.service.shezhiservice.ShangPinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author weishen
 */
@Service
public class ShangPinServiceImpl implements ShangPinService {

    @Autowired
    ShangPinMapper1 shangPinMapper;

    @Override
    public R selectAllShangPin() {
        R r= new R();
        List<ShangPin> shangPins = shangPinMapper.selectAllShangPin();
        r.setCode(200);
        r.setCount(shangPinMapper.selectCountShangPin());
        r.setMsg("查询成功！");
        r.setData(shangPins);
        return r;
    }

    @Override
    public int selectCountShangPin() {
        return shangPinMapper.selectCountShangPin();
    }

    @Override
    public int selectCountAnyShangPin(String shangPinBianHao, String shangPinMingCheng, String guiGeXingHao) {
       return shangPinMapper.selectCountAnyShangPin(shangPinBianHao,shangPinMingCheng,guiGeXingHao);
    }

    @Override
    public R selectAllShangPinForPage(int page, int size) {
        R r= new R();
        int index = (page - 1)*size;
        List<ShangPin> shangPins = shangPinMapper.selectAllShangPinForPage(index,size);
        r.setCode(200);
        r.setCount(shangPinMapper.selectCountShangPin());
        r.setMsg("查询成功！");
        r.setData(shangPins);
        return r;
    }

    @Override
    public R selectShangPinByBianHaoOrMingChengOrGuiGeXingHao(int page, int size, String shangPinBianHao, String shangPinMingCheng, String guiGeXingHao) {
        R r= new R();
        int index = (page - 1)*size;
        List<ShangPin> shangPins = shangPinMapper.selectShangPinByBianHaoOrMingChengOrGuiGeXingHao(index,size,shangPinBianHao,shangPinMingCheng,guiGeXingHao);
        r.setCode(200);
        r.setCount(selectCountAnyShangPin(shangPinBianHao,shangPinMingCheng,guiGeXingHao));
        r.setMsg("查询成功！");
        r.setData(shangPins);
        return r;
    }

    @Override
    public R insertShangPin(ShangPin_SheZhi shangPin) {
        R r = new R();
        int res = shangPinMapper.insertShangPin(shangPin);
        r.setCode(500);
        r.setMsg("新增失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("新增成功");
        }
        return r;
    }

    @Override
    public R updateShangPinById(ShangPin_SheZhi shangPin) {
        R r = new R();
        int res = shangPinMapper.updateShangPinById(shangPin);
        r.setCode(500);
        r.setMsg("更新失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("更新成功");
        }
        return r;
    }

    @Override
    public R deleteShangPinById(ArrayList<Integer> id) {
        R r = new R();
        int res=0;
        int flag=1;
        for(int i=0;i<id.size();i++)
        {
            res=shangPinMapper.deleteShangPinById(id.get(i));
            if(res==0)
            {
                flag=0;
                break;
            }
        }
        r.setCode(200);
        r.setMsg("删除成功");
        if(flag==0)
        {
            r.setCode(500);
            r.setMsg("删除失败");
        }
        return r;
    }
}
